package wechat;

import enums.DeletedStatus;
import enums.Gender;
import models.base.BaseModel;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by shancheng on 16-6-17.
 */
@Entity
@Table(name = "power_web_users")
public class WebUser extends BaseModel {


    /**
     * 微信用户名，fromUserName.
     * TODO: 加上索引.
     */
    @Column(name = "openId", length = 50)
    public String openId;

    /**
     *
     */
    @Column(name = "sessionKey", length = 50)
    public String sessionKey;

    @Column(name = "union_id", length = 50)
    public String unionId;

    /**
     * 二维码
     */
    @Column(name="qr_code")
    public String qrCode ;

    /**
     * 微信确认身份用
     */
    @Column(name = "uuid")
    public String uuid;

    /**
     * 是否订阅.
     * 用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息。
     * 可考虑通过一个定时任务查询当前公众号的用户订阅状态.
     */
    @Column(name = "subcribed")
    public Boolean subcribed;

    /**
     * 用户头像路径.
     */
    @Column(name = "head_img_url", length = 200)
    public String headImgUrl;

    /**
     * 用户的昵称
     */
    @Column(name = "nick_name", length = 50)
    public String nickName;

    /**
     * 姓名
     */
    @Column(name = "real_name")
    public String realName;

    /**
     * 联系电话
     */
    @Column(name = "phone")
    public String phone;

    @Enumerated(EnumType.STRING)
    @Column(name = "sex", length = 10)
    public Gender sex;


    @Column(name = "birthDay")
    @Temporal(TemporalType.DATE)
    public Date birthDay;

    /**
     * 用户语言
     */
    @Column(name = "user_lang", length = 20)
    public String language;

    /**
     * 市
     */
    @Column(name = "city", length = 20)
    public String city;


    /**
     * 省份
     */
    @Column(name = "province", length = 20)
    public String province;

    /**
     * 国家
     */
    @Column(name = "country", length = 20)
    public String country;

    /**
     * 纬度
     */
    @Column(name = "latitude")
    public String latitude ;

    /**
     * 经度
     */
    @Column(name = "longitude")
    public String longitude ;

    /**
     *  当前所在省份
     */
    @Column(name = "current_province")
    public String currentProvince ;

    /**
     *  当前所在城市
     */
    @Column(name = "current_city")
    public String currentCity ;

    /**
     *  所在 区 / 县 / 镇
     */
    @Column(name = "current_region")
    public String currentRegion ;


    /**
     * 是否第一次进入
     */
    @Column(name = "is_first")
    public Boolean isFirst;

    @Column(name = "sign")
    public String sign;



    //DELIVERY 默认自提地址
    @Column(name="delivery_address")
    public String deliveryAddress;

    /**
     * 账户余额
     */
    @Column(name = "balance" , columnDefinition = "decimal(15,2)")
    public BigDecimal balance  = BigDecimal.ZERO;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;


    /**
     * 根据ID查询
     * @param userId
     * @return
     */
    public static WebUser findByUserId(String userId){
        return WebUser.find(" id = ?1 and deleted = ?2" , userId , DeletedStatus.UN_DELETED).first();
    }

    /**
     *
     * @param openId
     * @return
     */
    public static WebUser findByOpenId(String openId ){
        return WebUser.find("openId = ?1 and deleted = ?2" , openId ,DeletedStatus.UN_DELETED).first();
    }

    public static WebUser findByPhone(String phone) {
        return WebUser.find("phone = ?1 and deleted = ?2",phone,DeletedStatus.UN_DELETED).first();
    }


}
